/**
 * @file engineer_mode_voice_wakeup.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_ENGINEER_MODE!= 0
/**only for craneg A1**/
#if defined(USE_LV_VAD) && (USE_LV_VAD > 0)
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
bool voice_wakeup_on = false;

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void voice_wakeup_prepare_destory(lv_obj_t * activity_obj);
static lv_res_t voice_wakeup_switch_signal(lv_obj_t * sw, lv_signal_t sign, void * param);
static bool get_voice_wakeup_setting(void);
static void set_voice_wakeup_setting(bool on);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_signal_cb_t old_sw_signal;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * engineer_mode_voice_wakeup_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ENGINEER_MODE_VOICE_WAKEUP;
        activity_ext.create = engineer_mode_voice_wakeup_create;
        activity_ext.prepare_destory = voice_wakeup_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    lv_obj_t * title = lv_label_create(obj, NULL);
    lv_obj_set_size(title, LV_HOR_RES, LV_VER_RES / 4);
    lv_label_set_text_id(title, WATCH_TEXT_ID_ENGINEER_MODE_VOICE_WAKEUP);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_obj_align(title, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);

    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_set_size(content, LV_HOR_RES, LV_VER_RES / 6);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_watch_cont_opa1);
    lv_obj_set_click(content, false);
    lv_obj_align(content, title, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_t * sw_label = lv_label_create(content, NULL);
    lv_label_set_text_id(sw_label, WATCH_TEXT_ID_ENGINEER_MODE_VOICE_WAKEUP);
    lv_obj_align(sw_label, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 5);
    lv_obj_add_style(sw_label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_t * sw = lv_switch_create(content, NULL);
    lv_obj_set_size(sw, LV_HOR_RES / 4, 30);
    lv_obj_align(sw, NULL, LV_ALIGN_IN_TOP_RIGHT, -10, 5);
    old_sw_signal = lv_obj_get_signal_cb(sw);
    lv_obj_set_signal_cb(sw, voice_wakeup_switch_signal);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_CHECKED, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
    if(get_voice_wakeup_setting()) {
        lv_switch_on(sw, LV_ANIM_OFF);
    } else {
        lv_switch_off(sw, LV_ANIM_OFF);
    }

    lv_obj_t * note_label = lv_label_create(obj, NULL);
    lv_obj_add_style(note_label, LV_LABEL_PART_MAIN, &lv_watch_font30_yellow);
    lv_label_set_long_mode(note_label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(note_label, lv_obj_get_width(obj) - 40);
    lv_label_set_text_id(note_label, WATCH_TEXT_ID_EMODE_VAD_SETTING_TIP);
    lv_obj_align(note_label, content, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

    return obj;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void voice_wakeup_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static lv_res_t voice_wakeup_switch_signal(lv_obj_t * sw, lv_signal_t sign, void * param)
{
    if(sign == LV_SIGNAL_RELEASED) {
        if(lv_switch_get_state(sw)) {
            lv_switch_off(sw, LV_ANIM_ON);
            Hal_Voice_Wakeup_Vad_Off();
            set_voice_wakeup_setting(false);
        } else {
            lv_switch_on(sw, LV_ANIM_ON);
            Hal_Voice_Wakeup_Vad_On();
            set_voice_wakeup_setting(true);
        }
        return LV_RES_OK;
    }

    return old_sw_signal(sw, sign, param);
}

static void set_voice_wakeup_setting(bool on)
{
    voice_wakeup_on = on;
}

static bool get_voice_wakeup_setting(void)
{
    return voice_wakeup_on;
}

#endif /*USE_LV_VAD*/
#endif /*USE_LV_WATCH_ENGINEER_MODE*/
